;(function(window, $) {
  var page1 = paging()

  // how many on the page
  var limit = 3
  var nowPage = 1

  $(function() {
    page1.init(
      1,
      0,
      $('#page1'),
      function() {
        getList($(this).data('page'))
      },
      limit
    )
    getList(1)
  })

  function getList(page) {
    nowPage = page
    util.ajax({
      url: '/manager/recommend/list?limit=' + limit + '&page=' + page,
      type: 'POST',
      success: function(res) {
        console.log(res)
        page1.init(page, res.data.total)
        $('.rec-ulwrap').html('')

        var remObj = res.data.rows
        for (var i in remObj) {
          var li = $('<li class="rec-cont"></li>').appendTo($('.rec-ulwrap'))
          var img = $('<img class="user-del" alt="">')
            .attr('src', remObj[i].avatar)
            .appendTo(li)
          var div2 = $('<div class="user-del user-info"></div>').appendTo(li)
          var ul = $('<ul></ul>').appendTo(div2)
          if (remObj[i].name == undefined) {
            var li1 = $('<li></li>')
              .html('姓名：' + '<span>' + '--' + '</span>')
              .appendTo(ul)
          } else {
            var li1 = $('<li></li>')
              .html('姓名：' + '<span>' + remObj[i].name + '</span>')
              .appendTo(ul)
          }

          // //loanAmount

          // var json1=["1","2","3","4","5","10","15","20","30","50","100","200","300"];

          // if(remObj[i].loanAmount==undefined){
          //     var li2=$('<li></li>').html("意向资金："+"<span>"+"--"+"<i>万元</i>"+"</span>").appendTo(ul);
          // }else{
          //     var lKey=remObj[i].loanAmount;
          //     var loanmoney=json1[lKey];
          //     var li2=$('<li></li>').html("意向资金："+"<span>"+loanmoney+"<i>万元</i>"+"</span>").appendTo(ul);
          // }

          //productName

          var li3 = $('<li></li>')
            .html('心仪产品：' + '<span>' + remObj[i].productName + '</span>')
            .appendTo(ul)

          //professional

          var json2 = ['自由职业者', '个体户', '企业主', '上班族']
          if (remObj[i].professional == undefined) {
            var li4 = $('<li></li>')
              .html('身份：' + '<span>' + '--' + '</span>')
              .appendTo(ul)
          } else {
            var pKey = remObj[i].professional
            var professional = json2[pKey]
            var li4 = $('<li></li>')
              .html('身份：' + '<span>' + professional + '</span>')
              .appendTo(ul)
          }

          //phone
          if (remObj[i].telPhone) {
            var telPhone =
              remObj[i].telPhone.substring(0, 3) +
              '****' +
              remObj[i].telPhone.substring(7, 11)
          } else {
            var telPhone = ''
          }

          var li5 = $('<li></li>')
            .html('手机号码：' + '<span>' + telPhone + '</span>')
            .appendTo(ul)

          //houseType  carType

          if (remObj[i].houseType == 1 && remObj[i].carType == 1) {
            var div3 = $('<div class="user-del user-cond"></div>')
              .html(
                "<span class='house'>有房</span><span class='car'>有车</span>"
              )
              .appendTo(li)
          } else if (remObj[i].houseType == 0 && remObj[i].carType == 0) {
            var div3 = $('<div class="user-del user-cond"></div>').appendTo(li)
          } else if (remObj[i].houseType == 1 && remObj[i].carType == 0) {
            var div3 = $('<div class="user-del user-cond"></div>')
              .html("<span class='house'>有房</span>")
              .appendTo(li)
          } else if (remObj[i].houseType == 0 && remObj[i].carType == 1) {
            var div3 = $('<div class="user-del user-cond"></div>')
              .html("<span class='car'>有车</span>")
              .appendTo(li)
          } else if (
            remObj[i].houseType == undefined &&
            remObj[i].carType == undefined
          ) {
            var div3 = $('<div class="user-del user-cond"></div>').appendTo(li)
          }

          //receivedStatus
          //领取状态 0:未领取 1：已领取 2：超时作废 3：已被领取
          var div4 = $('<div class="user-del user-btn"></div>').appendTo(li)
          if (remObj[i].receivedStatus == 0) {
            var isreceived = '领取'
            var a = $('<a class="receivebtn"></a>')
              .html(isreceived)
              .appendTo(div4)
              .css('background', '#1a6bd9')
              .attr('data-id', remObj[i].productId)
          } else if (remObj[i].receivedStatus == 1) {
            var isreceived = '已领取'
            var a = $('<a class="receivebtn"></a>')
              .html(isreceived)
              .appendTo(div4)
              .css('background', '#bfbfbf')
              .attr('data-id', remObj[i].productId)
          } else if (remObj[i].receivedStatus == 2) {
            var isreceived = '超时作废'
            var a = $('<a class="receivebtn"></a>')
              .html(isreceived)
              .appendTo(div4)
              .css('background', '#bfbfbf')
              .attr('data-id', remObj[i].productId)
          } else if (remObj[i].receivedStatus == 3) {
            var isreceived = '已被领取'
            var a = $('<a class="receivebtn"></a>')
              .html(isreceived)
              .appendTo(div4)
              .css('background', '#bfbfbf')
              .attr('data-id', remObj[i].productId)
          }
        }

        bindAction()
      }
    })
  }

  function bindAction() {
    $('.receivebtn').on('click', function() {
      if ($(this).html() === '已被领取') {
        layer.alert('该客户已被领取，您来晚了！', {
          skin: 'demo-class',
          closeBtn: 0
        })
        // alert('该客户已被领取，您来晚了！')
        return
      } else if ($(this).html() === '超时作废') {
        layer.alert('超时未领取的客户，将被重新分配！', {
          skin: 'demo-class',
          closeBtn: 0
        })
        // alert('超时未领取的客户，将被重新分配！')
        return
      } else {
        var id = $(this).data('id')
        var thisCus = $(this)

        util.ajax({
          url: '/manager/recommend/list?limit=' + 99999 + '&page=1',
          type: 'POST',
          success: function(res) {
            var arr = res.data.rows
            var canReceive = false
            for (var i = 0, item; (item = arr[i++]); ) {
              if (parseInt(item.id) === parseInt(id)) {
                canReceive = true
              }
            }

            if (!canReceive) {
              getList(nowPage)
              layer.alert('该客户已被领取，您来晚了！', {
                skin: 'demo-class',
                closeBtn: 0
              })
              // alert('该客户已被领取，您来晚了！')
              return
            }

            // send recevie request
            util.ajax({
              url: '/manager/recommend/receive/' + id,
              type: 'get',
              success: function(res) {
                layer.alert(
                  '恭喜您领取成功！',
                  {
                    skin: 'demo-class',
                    closeBtn: 0
                  },
                  function() {
                    location.reload()
                  }
                )
                // alert('恭喜您领取成功！')
                // thisCus.html('已被领取').css('background', '#bfbfbf')
              }
            })
          }
        })
      }
      return false
    })
  }
})(window, jQuery)
